package com.ffcs.web.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.util.URIUtil;
import org.apache.commons.lang.StringUtils;

public class HttpGetUtil {
	/** 
     * 执行一个HTTP GET请求，返回请求响应的HTML 
     * 
     * @param url                 请求的URL地址 
     * @param queryString 请求的查询参数,可以为null 
     * @return 返回请求响应的HTML 
     */ 
    public static String doGet(String url, String queryString) { 
            String response = null; 
            HttpClient client = new HttpClient(); 
            HttpMethod method = new GetMethod(url); 
            try { 
                    if (StringUtils.isNotBlank(queryString)) 
                            method.setQueryString(URIUtil.encodeQuery(queryString)); 
                    client.executeMethod(method); 
                    if (method.getStatusCode() == HttpStatus.SC_OK) {
                        response = method.getResponseBodyAsString(); 
                    } 
            }  catch (Exception e) { 
                    throw new RuntimeException(e.getMessage());
            } finally { 
                    method.releaseConnection();
            } 
            return response; 
    }
    
    /**
     * @Title: doGet2 
     * @Description: 解决 Going to buffer response body of large or unknown size. 
     * 				 Using getResponseBodyAsStream instead is recommended.的问题
     * @param :@param url
     * @param :@param queryString
     * @param :@return
     * @return String    返回类型  
     * @author 赵应光
     * @date 2014年6月18日 下午2:46:11
     */
    public static String doGet2(String url, String queryString) { 
        String response = null; 
        HttpClient client = new HttpClient(); 
        HttpMethod method = new GetMethod(url); 
        try { 
                if (StringUtils.isNotBlank(queryString)) 
                        method.setQueryString(URIUtil.encodeQuery(queryString)); 
                client.executeMethod(method); 
                if (method.getStatusCode() == HttpStatus.SC_OK) {
                	InputStream resStream = method.getResponseBodyAsStream();  
                    BufferedReader br = new BufferedReader(new InputStreamReader(resStream));  
                    StringBuffer resBuffer = new StringBuffer();  
                    String resTemp = "";  
                    while((resTemp = br.readLine()) != null){  
                        resBuffer.append(resTemp);  
                    }  
                    response = resBuffer.toString();  
                } 
        }  catch (Exception e) { 
                throw new RuntimeException(e.getMessage());
        } finally { 
                method.releaseConnection();
        } 
        return response; 
}
}
